﻿var model = avalon.define({
    $id: 'slider',
    new_slider: {
        FirstPicture: null
    },
    for_edit_slider: null,
    page: 1,
    pages: null,
    slider_list: [],
    get_slider_list: function (page_index) {
        page_index = page_index || 1;
        $.get('/site/getslider', { pageIndex: page_index, pageSize: 2 }).success(model.on_get_slider_list_success);
    },
    on_get_slider_list_success: function (datas) {
        model.slider_list = datas.Datas;
        if (model.pages === null) {
            model.pages = Math.ceil(datas.DataCount/datas.PageSize);
            page_list('#list_page', model.pages, model.get_slider_list);
        }
    },
    show_first_pictrue: function () {
        $('#first_picture_preview').modal('show');
    },
    on_picture_edit_callback: function (new_path) {
        this.new_slider.FirstPicture = new_path;
        layer.closeAll();
    },
    edit_first_pictrue: function () {
        model.open_window_to_eidt_first_picture();
    },
    open_window_to_eidt_first_picture: function () {
        var index = layer.open({
            type: 2,
            content: '/image/edit?path=' + this.new_slider.FirstPicture,
            area: ['100%', '100%'],
            maxmin: true
        });
        layer.full(index);
    },
    save_new_slider: function () {
        $.post('/site/savenewslider', model.new_slider).success(model.on_save_new_slider_success);
    },
    on_save_new_slider_success: function () {
        model.new_slider = {};
    },
    edit_slider_picture: function (picture_path) {
        var index = layer.open({
            type: 2,
            content: '/image/edit?path=' + picture_path,
            area: ['100%', '100%'],
            maxmin: true
        });
        layer.full(index);
    },
    remove_slider: function (slider) {
        $.post('/site/removeslider', { sliderID: slider.ID }).success(model.on_remove_slider_success);
    },
    on_remove_slider_success: function () {
        model.get_slider_list();
    },
    replace_slider_first_image: function (el) {
        model.for_edit_slider = el;
        var upload_param = { url: '/image', fieldName: 'fileData' }
        new FileUpload(upload_param, file.files[0]).upload(model.file_upload_success);
    },
    on_replace_first_image_upload_success: function (res) {
        model.for_edit_slider.FirstPicture = res.Path;
    }
});

model.file_upload_success = function (res) {
    model.new_slider.FirstPicture = res.Path;
    model.open_window_to_eidt_first_picture();
}

function upload_file(file) {
    var upload_param = { url: '/image', fieldName: 'fileData' }
    new FileUpload(upload_param, file.files[0]).upload(model.file_upload_success);
}

function on_picture_edit_callback(new_path) {
    model.on_picture_edit_callback(new_path);
}

//******
model.get_slider_list();

